<template>
	<view class="content" :style="app_bg">
		<view class="topTitle" :style="'top:'+zhuangtai+'px'">
			<image class="back" @click="backClick" :src="publicimg.btn_back" mode=""></image>
			<view class="" style="font-size: 32rpx;">
				分销中心
			</view>
		</view>
		<view class="dis-center" :style="'margin-top:'+(zhuangtai+52)+'px;'">
			<image class="top-image" src="../../static/fenxiao_top.png" mode=""></image>+';'
			<view class="condition-item" v-if="is_invite == 1" :style="app_theme">
				<text class="font34" :style="font_1st">条件：邀请{{invite_amount}}名新用户</text>
				<view class="right-btn">
					<image class="bgimgpublic" style="z-index: 0;" :src="publicimg.btn_xs_warm" mode=""></image>
					<text class="font30" style="z-index: 1;" :style="font_1st">{{member_invite_amount}}/{{invite_amount}}</text>
				</view>
			</view>
			<view class="condition-item" v-if="is_sign == 1" :style="app_theme">
				<text class="font34" :style="font_1st">条件：签到{{sign_day}}天</text>
				<view class="right-btn">
					<image class="bgimgpublic" style="z-index: 0;" :src="publicimg.btn_xs_warm" mode=""></image>
					<text class="font30" style="z-index: 1;" :style="font_1st">{{member_sign_day}}/{{sign_day}}</text>
				</view>
			</view>
			<view class="condition-item" @click="lookAdVideo" v-if="is_advert == 1" :style="app_theme">
				<text class="font34" :style="font_1st">条件：观看视频{{advert_amount}}次</text>
				<view class="right-btn">
					<image class="bgimgpublic" style="z-index: 0;" :src="publicimg.btn_xs_warm" mode=""></image>
					<text class="font30" style="z-index: 1;" :style="font_1st">{{member_advert_amount}}/{{advert_amount}}</text>
				</view>
			</view>
			<view class="condition-item" v-if="is_level == 1" :style="app_theme">
				<text class="font34" :style="font_1st">条件：达到{{level.level}}级</text>
				<view class="right-btn">
					<image class="bgimgpublic" style="z-index: 0;" :src="publicimg.btn_xs_warm" mode=""></image>
					<text class="font30" style="z-index: 1;" :style="font_1st">{{member_level}}/{{level.level}}</text>
				</view>
			</view>
			<view class="dis-rule" :style="app_theme">
				<text class="dis-top-text font34" :style="font_1st+app_border_bottom">分销说明</text>
				<rich-text class="font28 rich-text" :style="font_3rd" :nodes="desc"></rich-text>
			</view>
			<view class="bottom-btn" v-if="is_complete !=1 && status == 0">
				<image class="bgimgpublic" style="z-index: 0;" :src="publicimg.btn_lg_primary" mode=""></image>
				<text class="font36 index-1" :style="font_1st">申请成为分销商</text>
			</view>
			<view class="bottom-btn" v-if="is_complete ==1 && status == 0" @click="postDis">
				<image class="bgimgpublic" style="z-index: 0;" :src="publicimg.btn_lg_warm" mode=""></image>
				<text class="font36 index-1" :style="font_1st">申请成为分销商</text>
			</view>
			<view class="bottom-btn" v-if="status == 1">
				<image class="bgimgpublic" style="z-index: 0;" :src="publicimg.btn_lg_disabled" mode=""></image>
				<text class="font36 index-1" :style="font_1st">审核中</text>
			</view>
			<view class="bottom-btn" v-if="status == 2">
				<image class="bgimgpublic" style="z-index: 0;" :src="publicimg.btn_lg_disabled" mode=""></image>
				<text class="font36 index-1" :style="font_1st">审核通过</text>
			</view>
		</view>
		<image @click="postPay" v-if="distribution_apply_pay == 1" class="to-pay-icon" src="../../static/topay.png" mode=""></image>
		<uni-popup ref="successDisGet" type="center" :maskClick="false">
			<view class="loadexchagepopup" :style="app_theme">
				<view class="exchagetop" :style="app_border_bg">
					<image @click="$refs.successDisGet.close()" class="closepopup" :src="publicimg.btn_close" mode=""></image>
					<text class="font36 index-1" style="margin-top:28rpx;" :style="font_1st">提示</text>
				</view>
				<text class="font30 text1" :style="font_1st">申请成功，等待审核中…</text>
				<view class="btnlist">
					<view class="btn" @click="$refs.successDisGet.close()">
						<image class="bgimgpublic" style="z-index: 0;" :src="publicimg.btn_xs_warm" mode=""></image>
						<text class="font30 index-1" :style="font_1st">确定</text>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	var util = require('@/js/util.js')
	export default {
		data() {
			return {
				publicTextColor:'',
				publicimg:'',
				zhuangtai:0,
				is_complete:0,
				status:0,
				is_invite:0,
				invite_amount:0,
				member_invite_amount:0,
				is_sign:0,
				sign_day:0,
				member_sign_day:0,
				is_advert:0,
				advert_amount:0,
				member_advert_amount:0,
				rewarded_ad_unit:0,
				is_level:0,
				level:'',
				member_level:0,
				distribution_apply_pay:0,
				distribution_apply_pay_money:0,
				desc:''
			};
		},
		onShow() {
			let that = this
			let aniosgaodu = 0
			uni.getSystemInfo({
				success: function(res) {
					console.log(res);
					that.zhuangtai = res.statusBarHeight;
					var a = res.model.split(' ');
					if (a[0] == 'iPhone') {
						aniosgaodu = 44;
					} else {
						aniosgaodu = 48;
					}
					that.zhuangtai = parseInt(that.zhuangtai)+4
					console.log(that.zhuangtai)
				}
			});
		},
		onLoad() {
			this.setcolorConfig()
			this.getDisIndex()
		},
		onShareAppMessage() { // 分享 buttom share
			return {
				title: '合成王者',
				path: '/pages/index/index?parent_id='+uni.getStorageSync('member_id')
			}
		},
		computed:{
			user_margin_top(){
				return `margin-top:${(this.zhuangtai+52)}px;background:${this.publicTextColor.app_theme_color};`
			},
			page_top(){
				return `margin-top:${(this.zhuangtai+52)}px;`
			},
			app_border_bg(){
				return `background:${this.publicTextColor.app_border_color};`
			},
			app_border_top(){
				return `border-top:2rpx solid ${this.publicTextColor.app_border_color};`
			},
			app_border_bottom(){
				return `border-bottom:2rpx solid ${this.publicTextColor.app_border_color};`
			},
			app_border(){
				return `border:2rpx solid ${this.publicTextColor.app_border_color};`
			},
			app_theme(){
				return `background:${this.publicTextColor.app_theme_color};`
			},
			app_bg(){
				return `background:${this.publicTextColor.app_bg_color};`
			},
			font_4th(){
				return `color:${this.publicTextColor.font_4th_color};`
			},
			font_3rd(){
				return `color:${this.publicTextColor.font_3rd_color};`
			},
			font_2nd(){
				return `color:${this.publicTextColor.font_2nd_color};`
			},
			font_1st(){
				return `color:${this.publicTextColor.font_1st_color};`
			}
		},
		methods:{
			postPay(){
				let _self = this
				uni.request({
					url: util.getUrl("distribution/pay"),
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'Authori-zation':uni.getStorageSync("token")
					},
					data:{
					},
					method: 'POST',
					success: res => {
						if(res.data.code == 1){
							uni.requestPayment({
								provider:'wxpay',
								timeStamp:res.data.data.timeStamp,
								nonceStr:res.data.data.nonceStr,
								package:res.data.data.package,
								signType:res.data.data.signType,
								paySign:res.data.data.sign,
								success: (res) => {
									console.log(res)
									setTimeout(()=>{
										_self.postDis()
									},300)
								}
							})
						}else{
							uni.showToast({
								title:res.data.msg,
								icon:'none'
							})
						}
					},
				});
			},
			getDisIndex(){
				let _self = this
				uni.request({
					url: util.getUrl("distribution/details"),
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'Authori-zation':uni.getStorageSync("token")
					},
					data:{
					},
					method: 'POST',
					success: res => {
						if(res.data.code == 1){
							_self.is_complete = res.data.data.is_complete
							_self.status = res.data.data.status
							_self.is_invite = res.data.data.is_invite
							_self.invite_amount = res.data.data.invite_amount
							_self.member_invite_amount = res.data.data.member_invite_amount
							_self.is_sign = res.data.data.is_sign
							_self.sign_day = res.data.data.sign_day
							_self.member_sign_day = res.data.data.member_sign_day
							_self.is_advert = res.data.data.is_advert
							_self.advert_amount = res.data.data.advert_amount
							_self.member_advert_amount = res.data.data.member_advert_amount
							_self.rewarded_ad_unit = res.data.data.rewarded_ad_unit
							_self.is_level = res.data.data.is_level
							_self.level = res.data.data.level
							_self.member_level = res.data.data.member_level
							_self.distribution_apply_pay = res.data.data.distribution_apply_pay
							_self.distribution_apply_pay_money = res.data.data.distribution_apply_pay_money
							_self.desc = res.data.data.desc
						}else{
							uni.showToast({
								title:res.data.msg,
								icon:'none'
							})
						}
					},
				});
			},
			postDis(){
				let _self = this
				uni.request({
					url: util.getUrl("distribution/apply"),
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'Authori-zation':uni.getStorageSync("token")
					},
					data:{
					},
					method: 'POST',
					success: res => {
						if(res.data.code == 1){
							_self.$refs.successDisGet.open()
							_self.getDisIndex()
						}else{
							uni.showToast({
								title:res.data.msg,
								icon:'none'
							})
						}
					},
				});
			},
			postDisVideo(){
				let _self = this
				uni.request({
					url: util.getUrl("distribution/ad"),
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'Authori-zation':uni.getStorageSync("token")
					},
					data:{
					},
					method: 'POST',
					success: res => {
						if(res.data.code == 1){
							_self.getDisIndex()
						}else{
							uni.showToast({
								title:res.data.msg,
								icon:'none'
							})
						}
					},
				});
			},
			lookAdVideo(){  //type :1 复活视频 2 翻倍视频
				let _self = this
				if(_self.openVideo == null){
					_self.openVideo = uni.createRewardedVideoAd({
						adUnitId:_self.rewarded_ad_unit,
					})
					_self.openVideo.load((resLoad)=>{
						console.log("adload",resLoad)
					})
					_self.openVideo.onError(function(eError){
						console.log("eError",eError)
					})
					_self.openVideo.onClose((res)=>{
						if(res && res.isEnded){
							_self.postDisVideo()
						}
					})
				}
				clearInterval(_self.rebornIntervar)
				_self.openVideo.show()
					.catch(() => {
						_self.openVideo.load()
							.then(() => {
								_self.openVideo.show()
							})
							.catch(err => {
								console.log('激励视频 广告显示失败')
								uni.showToast({
									title: '暂无激励视频',
									icon: 'none',
									duration: 2000
								});
							})
					})
			},
			setcolorConfig(){
				this.publicTextColor = uni.getStorageSync("diyColor")
				this.publicimg = uni.getStorageSync("diyImg")
			},
			backClick(){
				uni.navigateBack({
					
				})
			}
		}
	}
</script>

<style lang="scss">
	.loadexchagepopup{
		display: flex;
		align-items: center;
		flex-direction: column;
		padding-bottom: 40rpx;
		border-radius: 15rpx;
		position: relative;
		width: 690rpx;
		.textrule{
			box-sizing: border-box;
			padding: 3rpx 56rpx 46rpx 51rpx;
			height: 500rpx;
		}
		.text1{
			text-align: center;
			line-height:45rpx;
		}
		.btnlist{
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 45rpx;
			.btn{
				display: flex;
				align-items: center;
				justify-content: center;
				width: 200rpx;
				height: 60rpx;
				position: relative;
			}
		}
		.centerimage{
			width: 265rpx;
			height: 247rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-bottom: 43rpx;
			position: relative;
			.exchangegoodsimage{
				width: 127rpx;
				height: 169rpx;
				background-color: #FFFFFF;
				z-index: 1;
			}
		}
		.exchagetop{
			position: relative;
			width: 690rpx;
			height: 80rpx;
			display: flex;
			justify-content: center;
			border-radius: 15rpx 15rpx 0 0;
			margin-bottom: 43rpx;
			.closepopup{
				position: absolute;
				right: 30rpx;
				top:30rpx;
				height: 31rpx;
				width: 31rpx;
			}
			.imagetop{
				position: absolute;
				top: 0;
				z-index: 0;
				height: 92rpx;
				width: 445rpx;
			}
		}
	}
	.to-pay-icon{
		width: 90upx;
		height: 90upx;
		position: fixed;
		right: 24upx;
		z-index: 2;
		bottom: 250upx;
	}
	.dis-center{
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 30upx;
		box-sizing: border-box;
		margin-bottom: 30upx;
		.bottom-btn{
			display: flex;
			align-items: center;
			justify-content: center;
			position: relative;
			width: 690upx;
			height: 80upx;
			position: fixed;
			z-index: 3;
			bottom: 60upx;
		}
		.dis-rule{
			display: flex;
			align-items: center;
			flex-direction: column;
			width: 690upx;
			padding: 24upx 0 30upx 0;
			box-sizing: border-box;
			border-radius: 20upx;
			margin-bottom: 30upx;
			.rich-text{
				width: 630upx;
				margin-top: 30upx;
			}
			.dis-top-text{
				width: 100%;
				text-align: center;
				padding-bottom: 24upx;
			}
		}
		.top-image{
			width: 690upx;
			height: 200upx;
			margin-bottom: 30upx;
		}
		.condition-item{
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 30upx;
			border-radius: 20upx;
			width: 100%;
			height: 100upx;
			box-sizing: border-box;
			padding: 33upx 30upx;
			.right-btn{
				display: flex;
				align-items: center;
				justify-content: center;
				position: relative;
				width: 150upx;
				height: 48upx;
			}
		}
	}
	.content{
		min-height: 100vh;
		width: 750rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		box-sizing: border-box;
		position: relative;
	}
	.topTitle{
		color: #FFFFFF;
		display: flex;
		align-items: center;
		height: 67rpx;
		justify-content: center;
		position: absolute;
		z-index: 10;
		width: 100%;
		.back{
			width: 20rpx;
			height: 31rpx;
			position: absolute;
			left: 31rpx;
		}
	}
</style>
